using System;
using System.Text;
using System.Windows.Forms;
using WindowsLive.Writer.Api;

namespace WindowsLive.Writer.Plugin.TimeSpanPlugin
{
    [WriterPlugin("24aabaa9-53db-4361-916d-32f9fee9711a", "Time Span",
        ImagePath = "Images.icon.png",
        PublisherUrl = "http://www.microsoft.com",
        HasEditableOptions = true,
        Description = "Embed elapsed time between 2 dates in your blog posts.")]

    [InsertableContentSource("Time Span")]
    public class ElapsedTime : ContentSource
    {
        #region Constructor
        public ElapsedTime()
        {
        } 
        #endregion

        public override void EditOptions(IWin32Window dialogOwner)
        {
            using (InputForm form = new InputForm(true))
            {
                DialogResult res = form.ShowDialog(dialogOwner);
            }
        }

        /// <summary>
        /// Creates the content.
        /// </summary>
        /// <param name="dialogOwner">The dialog owner.</param>
        /// <param name="newContent">The new content.</param>
        /// <returns></returns>
        public override DialogResult CreateContent(IWin32Window dialogOwner, ref string newContent)
        {
            using (InputForm form = new InputForm())
            {
                DialogResult res = form.ShowDialog(dialogOwner);
                if (res == DialogResult.OK)
                {
                    newContent = GetTimeSpan(form.OutputFormat, form.StartDate, form.EndDate, form.TimeInterval);
                }
                return res;
            }            
        }

        private string GetTimeSpan(string outputFormat, DateTime startDate, DateTime endDate, TimeIntervalEnum timeInterval)
        {
            double timeSpan = DateLibrary.DateDiff(startDate, endDate, timeInterval);
            return String.Format(outputFormat, timeSpan.ToString());
        }
    }
}
